package com.xiaoyi.controller;

import com.xiaoyi.common.constant.CommonEnum;
import com.xiaoyi.entity.ResultBody;
import com.xiaoyi.entity.SysUser;
import com.xiaoyi.service.SysUserService;
import com.xiaoyi.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private SysUserService userService;

    @GetMapping("/getList")
    public ResultBody userList(@RequestHeader(required = false)String token){
        if (StringUtils.hasText(token)){
            Claims claims = JwtUtils.parseToken(token);
            System.out.println(claims);
            List<SysUser> users = userService.list();
            return ResultBody.success(users);
        }else {
            return ResultBody.error(CommonEnum.SIGNATURE_NOT_MATCH);
        }
    }

    @PostMapping("/login")
    public ResultBody login(){
        Map<String, Object> map = new HashMap<>();
        map.put("username", "extends");
        map.put("userId", "2435676534564");
        String token = JwtUtils.createToken(map);
        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        return ResultBody.success(result);
    }
}
